/*
 * Copyright 北京航空航天大学  @ 2015 版权所有
 */
package com.buaa.edu.leetcode.algorithm.twopoint;

import java.util.Arrays;

/**
 * <p>
 * 去除重复元素，每个元素可以允许出现
 * Given sorted array nums = [1,1,1,2,2,3],
 * return length = 5, 
 * with the first five elements of nums being 1, 1, 2, 2 and 3. 
 * It doesn't matter what you leave beyond the new length.
 * </p>
 * 
 * @author towan
 * @email tongwenzide@163.com
 * @time 2015年5月31日
 */
public class RemoveDuplicatesFromSortedArrayII {
    //此时返回结果为移除元素的个数
    public int removeDuplicates(int[] nums) {
        if(nums==null || nums.length<=2){
            return nums==null?0:nums.length;
        }   
        int len = nums.length;
       
        int first = 1;
        int second ;
        //second在前面走
        for(second=2;second<len;second++){
            //允许出现两次
            if(!(nums[first]==nums[second] && nums[second]==nums[first-1])){
                nums[++first] = nums[second];
            }
        }
        return first+1;
    }
    public static void main(String[] args) {
        RemoveDuplicatesFromSortedArrayII duplicatesFromSortedArray = new RemoveDuplicatesFromSortedArrayII();
        int[] nums = {1,1,1,2,3,3};
        System.out.println(duplicatesFromSortedArray.removeDuplicates(nums ));
        System.out.println(Arrays.toString(nums));
    }
}
